package com.transaction.demo.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

import com.transaction.demo.common.util.JsonUtils;
import com.transaction.demo.common.util.OkHttpUtil;
import com.transaction.demo.common.util.Result;
import com.transaction.demo.shop.config.ShopConfig;
import com.transaction.demo.shop.vo.BuyReqVo;

/**
 * 描述：支付下单
 * 作者：马庆智
 * 日期：2023/5/5 13:47
 */
@Api(tags = "支付下单")
@RestController
@RequestMapping("/pay")
@Slf4j
public class BuyController {

	@Autowired
	private ShopConfig config;

	/**
	 * 支付下单
	 *
	 * @return String
	 * @author 马庆智
	 * @date: 2023/5/5 13:49
	 */
	@ApiOperation("支付下单")
	@PostMapping("/testPay")
	public Result<String> testPay(@RequestBody @Valid BuyReqVo record) {
		String businessId = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());// 业务编号

		Map<String, Object> params = new HashMap<>();
		params.put("businessId", businessId); //业务编号
		params.put("productName", record.getProductName()); //商品名称
		params.put("price", record.getPrice()); //商品价格
		params.put("remark", record.getRemark()); //备注

		log.info("支付下单参数: {}", JsonUtils.toJson(params));
		try {
			log.info("支付下单参数：{},响应: {}", JsonUtils.toJson(params), OkHttpUtil.post(config.url, params));
		} catch (IOException e) {
			log.info("支付下单参数：{},异常: {}", JsonUtils.toJson(params), JsonUtils.toJson(e));
		}

		return Result.success("success");
	}

}
